package com.cn.aam.checaiduo.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.back.BackHandlerHelper;
import com.cn.aam.checaiduo.base.back.FragmentBackHandler;
import com.cn.aam.checaiduo.center.UserCenter;
import com.cn.aam.checaiduo.ui.useraccess.LoginActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * monker on 2017/5/12.
 */

public abstract class BaseFragment extends Fragment implements FragmentBackHandler {

    protected BaseActivity mActivity;
    private Unbinder unbinder;
    protected ViewGroup WMContent;


    /**
     * 获得全局的，防止使用getActivity()为空
     *
     * @param context
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (BaseActivity) context;
    }


    @Override
    public void onDetach() {
        super.onDetach();
        this.mActivity = null;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        WMContent = (ViewGroup) LayoutInflater.from(mActivity).inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, WMContent);
        ARouter.getInstance().inject(this);
        initView(WMContent, savedInstanceState);

        return WMContent;
    }


    /**
     * 获取控件的方法
     */
    public <T extends View> T myFindViewsById(int viewId) {
        View view = WMContent.findViewById(viewId);
        return (T) view;
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        mActivity.overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 该抽象方法就是 onCreateView中需要的layoutID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 该抽象方法就是 初始化view
     *
     * @param view
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle savedInstanceState);


    @Override
    public void onDestroy() {
        unbinder.unbind();
        super.onDestroy();

    }

    /**
     * 需要登录
     */
    public boolean needLogin() {
        if (!hasLogin() || !AppData.isGestureVerify) {
            goLogin();
            return true;
        }

        return false;
    }

    protected void goLogin() {
        UserCenter.getInstance().logOut();
        mActivity.startActivityForResult(LoginActivity.newInstance(mActivity), Const.INTENT_REQUEST_LOGIN);
        mActivity.overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.noting);
    }

    protected boolean hasLogin() {
        UserCenter userCenter = UserCenter.getInstance();
        boolean s = userCenter.isUserLogin();
        return userCenter.isUserLogin();
    }

    @Override
    public boolean onBackPressed() {
        return BackHandlerHelper.handleBackPress(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName()); //统计页面
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }
}